package com.google.code.rsser.model;

import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;

import net.sf.oval.constraint.NotEmpty;
import net.sf.oval.constraint.NotNull;

import com.google.code.rsser.web.validator.profiles.Profiles;

@Entity
public class Feed {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	protected int id;
	
	protected String url;
	
	@NotNull(errorCode = "feedUrl.notEmpty", profiles = { Profiles.SUBSCRIBE })
	@NotEmpty(errorCode = "feedUrl.notEmpty", profiles = { Profiles.SUBSCRIBE })
	protected String feedUrl;
	
	protected String title;
	protected String description;
	protected Date lastUpdate;

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "feed")
	@OrderBy(value = "date desc")
	protected List<Entry> entries;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "user_feed", joinColumns = { @JoinColumn(name = "id_feed") }, inverseJoinColumns = { @JoinColumn(name = "id_user") })
	protected List<User> subscribedBy;
	
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getLastUpdate() {
		return lastUpdate;
	}

	public void setLastUpdate(Date lastUpdate) {
		this.lastUpdate = lastUpdate;
	}

	public String getFeedUrl() {
		return feedUrl;
	}

	public void setFeedUrl(String feedUrl) {
		this.feedUrl = feedUrl;
	}

	public List<Entry> getEntries() {
		return entries;
	}

	public void setEntries(List<Entry> entries) {
		this.entries = entries;
	}

	public List<User> getSubscribedBy() {
		return subscribedBy;
	}

	public void setSubscribedBy(List<User> subscribedBy) {
		this.subscribedBy = subscribedBy;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		result = prime * result + ((url == null) ? 0 : url.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		
		if (obj == null) {
			return false;
		}
		
		if (!(obj instanceof Feed)) {
			return false;
		}
		
		Feed other = (Feed) obj;
		
		if (id != other.id) {
			return false;
		}
		
		if (url != null && !url.equals(other.url)) {
			return false;
		} else if (other.url != null) {
			return false;
		}
		
		return true;
	}
	
	@Override
	public String toString() {
		return "id=" + id + ", title=" + title;
	}
}
